<?php
/**
 * 轮播图模型
 */

namespace app\api\model;

use PDOStatement;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Model;

class Radar extends Model {
    /**
     * 雷达列表
     * @param $params
     * @param $pageSize
     */
    public function getRadarList($params,$pageSize){
        $data=db('agent_user_interaction_log')
            ->alias('a')
            ->field('a.id,a.type,a.house_id,a.itime,c.nickname,c.cover,b.id agent_user_id,b.status,b.remark,c.id user_id,a.num')
            ->join('erp_company_agent_user b','a.agent_user_id=b.id','left')
            ->join('erp_company_user c','b.user_id=c.id','left')
            ->where("b.agent_id={$params['agent_id']} and b.status in(1,2) and a.type!=12")
            ->page($params['page'],$pageSize)
            ->order('a.itime desc')
            ->select();

        $data=self::radarList($data);
        $count=db('agent_user_interaction_log')
            ->alias('a')
            ->join('erp_company_agent_user b','a.agent_user_id=b.id','left')
            ->join('erp_company_user c','b.user_id=c.id','left')
            ->where("b.agent_id={$params['agent_id']} and b.status in(1,2)")
            ->count();
        if ($pageSize > 0) {
            $pageNum = ceil($count / $pageSize);
        } else {
            $pageNum = 1;
        }
        $result['count'] = count($data);
        $result['page'] = (int)$params['page'];
        $result['pageNum'] = $pageNum;
        $result['statistical'] = $this->getCusStatistical($params);//客户统计
        $result['bannerer'] = $this->getRadarBanner();//客户统计
        $result['data'] = $data;

        return $result;

    }

    /**
     * 记录数据组装
     * @param $data
     * @return mixed
     */
    public static function radarList($data){

        if ($data){
            foreach ($data as $key =>$value){
                $data[$key]['nickname']=base64_decode($value['nickname']);
                $data[$key]['itime']=date('Y-m-d H:i',$value['itime']);
                $type_desc=self::interStyle($value['type'],$value['house_id'],$value['num']);
                $data[$key]['type_desc']=$type_desc;
            }
        }else{
            $data=[];
        }

        return $data;
    }

    /**
     * 互动记录描述
     * @param $type
     * @param $house_id
     * @return string
     */
    public static function  interStyle($type,$house_id,$num=1){
        $arr1=[1,2,3,4];
        $arr2=[5,6,7,8,11];
        $desc=Customer::$type_s[$type];
        if (in_array($type,$arr1)){
            if ($type==1){
                $type_desc= "<span style='color: #F8A31E'>{$desc}</span>";

            }else{
                $type_desc= "<span style='color: #000000'>{$desc}</span>";
            }
            if ($num>1&&in_array($type,[1,2])){
                $new_type_desc=str_replace('首次','',$type_desc);
                $type_desc=$new_type_desc;
            }
        }else if (in_array($type,$arr2)){
            $houseName = self::getHouseTitle($house_id);
            if ($type == 8) {
                $type_desc = "<span style='color: #000000'>询问了</span>"."<a style='color: #3693F2;text-decoration : none' href='".$house_id."'>{$houseName}</a>"."<a style='color: #000000'>的费用情况，点击去计算</a>";
            } else {

                $type_desc = "<span style='color: #000000'>{$desc}</span>"."<a style='color: #3693F2;text-decoration : none' href='".$house_id."'>{$houseName}</a>" ;
            }
        }else{
            $type_desc ="<span style='color: #000000'>{$desc}</span>";
        }
        return $type_desc;
    }

    /**.
     * 客户列表互动描述
     * @param $type
     * @param $house_id
     * @return string
     */
    public static function interCusStyle($type,$house_id,$num=1){
        $arr1=[1,2,3,4];
        $arr2=[5,6,7,8,11];
        $desc=Customer::$type_s[$type];
        if (in_array($type,$arr1)){
            $type_desc= "<span style='color: #F8A31E'>{$desc}</span>";
            if ($num>1&&in_array($type,[1,2])){
                $new_type_desc=str_replace('首次','',$type_desc);
                $type_desc=$new_type_desc;
            }
        }else if (in_array($type,$arr2)){
            $houseName = self::getHouseTitle($house_id);
            if ($type == 8) {
                $type_desc = "<span style='color: #F8A31E'>询问了</span>"."<a style='color: #3693F2;text-decoration : none' href='".$house_id."'>{$houseName}</a>"."<a style='color: #F8A31E'>的费用情况，点击去计算</a>";
            } else {

                $type_desc = "<span style='color: #F8A31E'>{$desc}</span>"."<a style='color: #3693F2;text-decoration : none' href='".$house_id."'>{$houseName}</a>" ;
            }
        }else{
            $type_desc ="<span style='color: #F8A31E'>{$desc}</span>";
        }
        return $type_desc;
    }


    /**
     * app雷达
     * @param $params
     * @return mixed
     */
    public function getCusStatistical($params){
        $time=date("Y-m-d",strtotime("-7 day"));
        $time7=strtotime($time);//前七天的时间戳
        //潜在客户总数
        $customer_total_number=db('company_agent_user')->where("agent_id={$params['agent_id']}")->count();
        //近七日新增潜在客户
        $customer_seven_number=db('company_agent_user')->where("agent_id={$params['agent_id']} and daytime_potential>{$time7}")->count();
        //专属客户总数
        $exclusive_customer_total_number=db('company_agent_user')->where("agent_id={$params['agent_id']} and status=2")->count();
        //近七日新增专属客户
        $exclusive_customer_seven_number=db('company_agent_user')->where("agent_id={$params['agent_id']} and status=2 and daytime_exclusive>{$time7}")->count();
        $info['customer_seven_number']=$customer_seven_number;//近七日新增潜在客户
        $info['customer_total_number']=$customer_total_number;//潜在客户总数
        $info['exclusive_customer_seven_number']=$exclusive_customer_seven_number;//近七日新增专属客户
        $info['exclusive_customer_total_number']=$exclusive_customer_total_number;//专属客户总数
        return $info;
    }

    /**
     * 雷达banner
     * @return mixed
     */
    public function getRadarBanner(){
        $info=db('banner')
            ->where('seat', 5)
            ->where('status', 1)
            ->select();
        return $info;
    }

    /**
     * 房源显示标题
     * @param int $house_id
     */
    public static function getHouseTitle($house_id=0){
        $house=db('house')
            ->alias('h')
            ->field('h.title,h.bedroom,h.livingroom,hv.name hv_name')
            ->join('erp_house_village hv','h.village_id=hv.id','left')
            ->where("h.id={$house_id}")
            ->find();
        $hv_name=$house['hv_name']??'';
        if ($house['bedroom']&&$house['livingroom']){
            $boor=$house['bedroom'].'室'.$house['livingroom'].'厅';
        }else{
            $boor='';
        }
        $title=$hv_name?$hv_name.' '.$boor:$house['title'];
        return $title;
    }


}
